<?php
namespace app\admin\controller;

use app\admin\model\admin\Admin as adminModel;
use Gregwar\Captcha\CaptchaBuilder;
use support\Request;

class Login extends Base
{

    public function index(Request $request)
    {
        $name = "大海";
        //$public_path = public_path();
        //$public_path ='http://127.0.0.1:8787/static';
        $public_path =$_SERVER['HTTP_HOST'].'/static';
        return view('login/index', ['name' => $name,'public_path'=>$public_path]);
    }

    /**
     * 输出验证码图像
     */
    public function captcha(Request $request)
    {
        // 初始化验证码类
        $builder = new CaptchaBuilder;
        // 生成验证码
        $builder->build();
        // 将验证码的值存储到session中
        $request->session()->set('captcha', strtolower($builder->getPhrase()));
        // 获得验证码图片二进制数据
        $img_content = $builder->get();
        // 输出验证码二进制数据
        return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
    }

    /**
     * 检查验证码
     */
    public function check(Request $request)
    {
        // 获取post请求中的captcha字段
        $captcha = $request->post('captcha');
        //账号
        $account = $request->post('account');
        //密码
        $pwd = $request->post('pwd');
        
        // 对比session中的captcha值
        if (strtolower($captcha) !== $request->session()->get('captcha')) {
            return json(['code' => 400, 'msg' => '输入的验证码不正确']);
        }
        return json(['code' => 0, 'msg' => 'ok']);
    }

}